<?php

    function PrintPdfLayoutNonPreV2($poNo){
        
    if($poNo !=null){
    require_once('tcpdf/config/lang/eng.php');
    require_once('tcpdf/tcpdf.php');
    $GLOBALS['pono'] = $poNo;//$this->poNo ;
    // Extend the TCPDF class to create custom Header and Footer
    //include('class/mypdf.class.php');
    //$mypdf = new MYPDF();
    //$mypdf->Header($GLOBALS['pono']);
    //$mypdf->Footer();
    
        class MYPDF extends TCPDF {
    
    
        	//Page header
        	public function Header() {
        		// Logo
        		//$image_file = K_PATH_IMAGES.'logo_example.jpg';
        		//$this->Image($image_file, 10, 10, 15, '', 'JPG', '', 'T', false, 300, '', false, false, 0, false, false, false);
        		// Set font
        		$this->SetFont('helvetica', 'B', 12);
        		// Title
               
                
                require('db_connector.php');
                //$connectDB = new mysql("localhost","root","asdfghjkl","edi_db_testsap","edidb","utf8");
        		//$result = $connectDB->conditionfind("po_hdr","where po_no='4700000110'");
                //$result2 = $connectDB->fetch_row($result);
                $sql ="select * from po_hdr where po_no='".$GLOBALS['pono']."'";
                $result = mysql_query($sql);
                $result2 = mysql_fetch_row($result);
        		$this->Cell(0, 10, $result2[50], 0, 1, 'C', 0, '', 0, false, 'M', 'M');
                
         		$this->SetFont('helvetica','I', 8);
        		$this->Cell(0, 10, $result2[51].",".$result2[52].",".$result2[53].",".$result2[54], 0, 1, 'C', 0, '', 0, false, 'M', 'M');
    		
            		// Position at 15 mm from bottom
    		$this->SetY(15);
            $this->SetX(190);
    		// Set font
    		$this->SetFont('helvetica', '', 8);
    		// Page number
    		//$this->Cell(0, 15, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
    		$this->Cell(0, 8, 'Page : '.$this->getAliasNumPage().' of '.$this->getAliasNbPages(), 0, 1, 'C', 0, '', 0, false, 'M', 'M');
            
            $this->SetX(130);
    		$this->SetFont('helvetica','', 9);
    		$this->Cell(0, 8, "Purchase Order No  :     ", 0, 1, 'C', 0, '', 0, false, 'M', 'M');
            $this->SetLineStyle(array('width' => 0.5, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)));
            $this->SetFillColor(255,255,255);
    //$this->SetTextColor(0,0,255);
    		$this->SetY(17);
            $this->SetX(177);
            $this->MultiCell(30, 4, $result2[2], 1, 'C', 1, 0);
            
    		$this->SetY(25);
    		$this->SetX(145);
            $this->SetFont('helvetica','', 9);
    		$this->Cell(0, 10, "Revision No. :    ".$result2[4], 0, 1, 'C', 0, '', 0, false, 'M', 'M');
            if($result2[33] =="C"){
    		$this->SetY(30);
    		$this->SetX(130);
            $this->SetFont('helvetica','B', 12);
    		$this->Cell(0, 10, "CANCELLED", 0, 1, 'C', 0, '', 0, false, 'M', 'M');  
            $this->SetTopMargin(36); 
            }
            
            else{
            $this->SetTopMargin(32);  
            }
            
    	}
    
    
    	// Page footer
    	public function Footer() {
    	   
    		// Position at 200 mm from bottom
            $this->SetX(200);
    		// Set font
    		$this->SetFont('helvetica', 'I', 8);
    		// Page number
    		$this->Cell(0, 0, 'SAP ', 0, false, 'C', 0, '', 0, false, 'T', 'M');
            
    	}

        }


    // create new PDF document
    $pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
    
    // set document information
    $pdf->SetCreator(PDF_CREATOR);
    $pdf->SetAuthor('Nicola Asuni');
    $pdf->SetTitle('TCPDF Example 003');
    $pdf->SetSubject('TCPDF Tutorial');
    $pdf->SetKeywords('TCPDF, PDF, example, test, guide');

    // set default header data
    $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
    
    // set header and footer fonts
    $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
    $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

    // set default monospaced font
    $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
    
    //set margins
    $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
    $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
    $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
    
    //set auto page breaks
    $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
    
    //set image scale factor
    $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
    
    //set some language-dependent strings
    $pdf->setLanguageArray($l);

    // ---------------------------------------------------------
    
    // set font
    $pdf->SetFont('times', '', 10);
    
    // add a page
    $pdf->AddPage();

    //$connectDB2 = new mysql("localhost","root","asdfghjkl","edi_db_testsap","edidb","utf8");
    //$result1 = $connectDB2->conditionfind("po_hdr","where po_no='4700000110'");
    //$resultbody = $connectDB2->fetch_row($result1);
            $sql ="select * from po_hdr where po_no='".$GLOBALS['pono']."'";
            $resultbd = mysql_query($sql);
            $resultbody = mysql_fetch_row($resultbd);
    
    // set some text to print
    
    //$result[] = $connectDB->show_tables(edi_db_testsap);
    //$poissue = date_format($resultbody[5], 'd F Y');
    $boardNo = "";
    $poPrintDate = date('d-M-Y');
    $poissue = new DateTime($resultbody[5]);
    $poissuec = $poissue->format('d-M-Y');
    $posendDate = new DateTime($resultbody[30]);
    $posendDatec = $posendDate->format('d-M-Y');
$html2 = <<<EOD
<table width="750" height="300" border="0">
  <tr>
    <td width="16%">Vendor/Pay To :</td>
    <td width="30%">$resultbody[36]<br>$resultbody[17]<br>$resultbody[18] <br>$resultbody[37], $resultbody[38]<br>$resultbody[40] </td>
    <td colspan="2" rowspan="3" width="10%">&nbsp;</td>
    <td width="10%">Ship To :</td>
    <td width="40%">$resultbody[22]<br>$resultbody[23]<br>$resultbody[24]<br>$resultbody[45],&nbsp;$resultbody[46],&nbsp;$resultbody[47]<br>$resultbody[48]</td>
  </tr>
  <tr>
  <br>
    <td>Factory :</td>
    <td>$resultbody[19]<br>$resultbody[20]<br>$resultbody[21]<br>$resultbody[41],&nbsp;$resultbody[42]<br>$resultbody[44]</td>
    <td>Mark For :</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
   <br>
    <td>Bill To :</td>
    <td>$resultbody[56]<br>$resultbody[57]<br>$resultbody[58]<br>$resultbody[59],&nbsp;$resultbody[60],&nbsp;$resultbody[61]<br>$resultbody[62]</td>
    <td>Fiscal Rep :</td>
    <td>&nbsp;</td>
  </tr>
  <br>
  <tr>
    <td>Agent/MO :</td>
    <td colspan="5">$resultbody[78]</td>
  </tr>
  <br>
  <tr>
    <td width="16%">Puechase Group :</td>
    <td width="20%">$resultbody[10]</td>
    <td width="15%">Customer PO# :</td>
    <td width="15%"><b>N/A</b></td>
    <td width="15%">PO Print :</td>
    <td width="15%">$poPrintDate</td>
  </tr>
  <tr>
    <td width="16%">Order Type :</td>
    <td width="20%">$resultbody[14]</td>
    <td width="15%">Storage Location :</td>
    <td width="15%">$resultbody[72]</td>
    <td width="15%">PO Issue :</td>
    <td width="15%">$poissuec</td>
  </tr>
    <tr>
    <td width="16%">PO Group :</td>
    <td width="20%">$resultbody[13]</td>
    <td width="15%">Mfg Origin :</td>
    <td width="15%">$resultbody[44]</td>
    <td width="15%">PO Contact :</td>
    <td width="15%">$resultbody[12]</td>
  </tr>
    <tr>
    <td width="16%">Payment Category :</td>
    <td width="20%">$resultbody[77]</td>
    <td width="15%">Business Type :</td>
    <td width="15%">$resultbody[15]</td>
    <td width="15%">Date Sent :</td>
    <td width="15%">$posendDatec</td>
  </tr>
</table>


EOD;
// print a block of text using Write()
//$pdf->writeHTMLCell($w=0, $h=0, $x='', $y='', $html, $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=true);
    $pdf->writeHTMLCell($w=0, $h=0, $x='2', $y='', $html2, $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=true);

    // add a page
    //require('mysql_connect.php');
    //$connectDB3 = new mysql("localhost","root","asdfghjkl","edi_db_testsap","edidb","utf8");
    
    //print_r($r);  
    //echo 
    //$texta = $r[1]['ItemLine_no'];
    
    //$itemLineNoresults = $connectDB2->querystm("SELECT ItemLine_no FROM po_dtl WHERE `PO_No` = '4700000110' UNION SELECT ItemLine_no FROM `po_dtl` WHERE `PO_No` = '4700000110'");
      $sqlbd3 ="SELECT ItemLine_no FROM po_dtl WHERE `PO_No` = '".$GLOBALS['pono']."' AND (`PkIndication` ='EA' or `PkIndication` ='PR' or `PkIndication` ='ST') UNION SELECT ItemLine_no FROM `po_dtl` WHERE `PO_No` = '".$GLOBALS['pono']."' AND (`PkIndication` ='EA' or `PkIndication` ='PR' or `PkIndication` ='ST')";
        $itemLineNoresults = mysql_query($sqlbd3);
        while ($row = mysql_fetch_array($itemLineNoresults)) {
        $r[] = $row;
        
        }
    $ordertotalamount = 0;
    $ordertotalqty = 0;
    $length = count($r);
    for ($i=0 ; $i<$length;$i++) {
    
    $itemlineno = $r[$i]['ItemLine_no'];
    
    
    $sqlbdtetail3 ="SELECT * FROM `po_dtl` WHERE `PO_No` ='".$GLOBALS['pono']."' AND `ItemLine_No` ='".$itemlineno. "' LIMIT 0 , 1";
    $itemLineNoresultsinfoq = mysql_query($sqlbdtetail3);
    $itemLineNoresultsinfo = mysql_fetch_row($itemLineNoresultsinfoq);
    
    
    
    
    $boardNo = $itemLineNoresultsinfo[31];
    $contractualDDate = new DateTime($itemLineNoresultsinfo[48]);
    $contractualDDatec = $contractualDDate->format('d-M-Y');
    
    $handoverDate  = new DateTime($itemLineNoresultsinfo[70]);
    $handoverDatec = $handoverDate->format('d-M-Y');
    
    
    if($itemLineNoresultsinfo[69] =="A"){
        $shipmode1 = "AIR";    
    }
    else if($itemLineNoresultsinfo[69] =="S"){
        $shipmode1 = "OCEAN";    
    }
    else if($itemLineNoresultsinfo[69] =="E"){
        $shipmode1 = "LAND";
    }
    
    if($itemLineNoresultsinfo[59] =="CC"){
        $shipmode2 = "COLLECT";    
    }
    else if($itemLineNoresultsinfo[59] =="PP"){
        $shipmode2 = "PRE-PAID";    
    }
    else{
        $shipmode2 = "";
    }
    
    
    $centralPOno = $itemLineNoresultsinfo[68];
    if($itemLineNoresultsinfo[68] ==""){
        $centralPOno = "<b>N/A</b>";
    }
    
    $customerHandoverPlace = $itemLineNoresultsinfo[66];
    if($itemLineNoresultsinfo[66] ==""){
        $customerHandoverPlace = "<b>N/A</b>";
    }
    
    $pattern = $itemLineNoresultsinfo[28];
    if($itemLineNoresultsinfo[28] ==""){
        $pattern = "<b>N/A</b>";
    }
    
    $fixture = $itemLineNoresultsinfo[42];
    if($itemLineNoresultsinfo[42] ==""){
        $fixture = "<b>N/A</b>";
    }
    
    $rigIndicator = $itemLineNoresultsinfo[27];
    if($itemLineNoresultsinfo[27] ==""){
        $rigIndicator = "<b>N/A</b>";
    }




$html3 = <<<EOD

<p></p>
<table width="750" height="300" border="0">
  <tr>
      <td colspan="6" style="background-color:#CDFFCE;">
Material Number&nbsp;:$itemLineNoresultsinfo[5]
</td>
  </tr>
  <tr>
    <td><u>PO Item # $itemLineNoresultsinfo[4]</u></td>
    <td>&nbsp;</td>
    <td colspan="4">&nbsp;</td>
  </tr>
  <tr>
    <td width="16%">Season :</td>
    <td width="15%">$itemLineNoresultsinfo[22]</td>
    <td width="13%">Incoterms :</td>
    <td width="20%">$itemLineNoresultsinfo[46]</td>
    <td width="19%">Contractual Delivery Date:</td>
    <td width="20%">$contractualDDatec</td>
  </tr>
  <tr>
    <td width="16%">Inbound Pkg :</td>
    <td width="15%">$itemLineNoresultsinfo[24]</td>
    <td width="13%">Incoterms Place :</td>
    <td width="20%">$itemLineNoresultsinfo[47]</td>
    <td width="19%">Handover Date :</td>
    <td width="20%">$handoverDatec</td>
  </tr>
  <tr>
    <td width="16%">Mfg Process :</td>
    <td width="15%">$itemLineNoresultsinfo[23]</td>
    <td width="13%">Harbor/Port :</td>
    <td width="20%">$itemLineNoresultsinfo[64]</td>
    <td width="19%">Customer Handover Place :</td>
    <td width="20%">$customerHandoverPlace</td>
  </tr>
  <tr>
  
    <td width="16%">Quality :</td>
    <td width="15%">$itemLineNoresultsinfo[26]</td>
    <td width="13%">Ship Mode :</td>
    <td width="20%">$shipmode1&nbsp;$shipmode2</td>
    <td width="15%">Shade :</td>
    <td width="20%">$itemLineNoresultsinfo[25]</td>
  </tr>
  <tr>
    <td width="16%">Central PO number :</td>
    <td width="15%">$centralPOno</td>
    <td width="13%">&nbsp;</td>
    <td width="20%">&nbsp;</td>
    <td width="15%">&nbsp;</td>
    <td width="20%">&nbsp;</td>
  </tr>
    <p></p>
  <tr>
    <td width="16%">Model :</td>
    <td width="15%">$itemLineNoresultsinfo[29]</td>
    <td width="13%">Product Type :</td>
    <td width="20%">$itemLineNoresultsinfo[39]</td>
    <td width="15%">Merch Division :</td>
    <td width="20%">$itemLineNoresultsinfo[36]</td>
  </tr>
  <tr>
    <td width="16%">Color Description :</td>
    <td width="15%">$itemLineNoresultsinfo[8]</td>
    <td width="13%">Class :</td>
    <td width="20%">$itemLineNoresultsinfo[45]</td>
    <td width="15%">Concept Short Desc :</td>
    <td width="20%">$itemLineNoresultsinfo[35]</td>
  </tr>
  <tr>
    <td width="16%">Fabric Content :</td>
    <td width="15%">$itemLineNoresultsinfo[33]</td>
    <td width="13%">Board :</td>
    <td width="20%">$itemLineNoresultsinfo[30]</td>
    <td width="15%">Fish &amp; Wildlife Ind :</td>
    <td width="20%">$itemLineNoresultsinfo[40]</td>
  </tr>
  <tr>
    <td width="16%">Pattern :</td>
    <td width="15%">$pattern</td>
    <td width="13%">Gender :</td>
    <td width="20%">$itemLineNoresultsinfo[37]</td>
    <td width="15%">Down Feather Ind :</td>
    <td width="20%">$itemLineNoresultsinfo[38]</td>
  </tr>
    <tr>
    <td width="16%">&nbsp;</td>
    <td width="15%">&nbsp;</td>
    <td width="13%">Fixture :</td>
    <td width="20%">$fixture</td>
    <td width="15%">Rig Indicator :</td>
    <td width="20%">$rigIndicator</td>
  </tr>
</table>

EOD;

    $pdf->writeHTMLCell($w=0, $h=0, $x='2', $y='', $html3, $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=true);


$htmlselling1 = <<<EOD
<table width="720" height="138" border="0" style="border-top: solid 1px #000;">
  <tr>
    <td colspan="7" align="center">Customer Selling</td>
  </tr>
  <tr>
    <td style="border-bottom: solid 1px #000;" align="center">Size</td>
    <td style="border-bottom: solid 1px #000;" align="center">UPC/EAN</td>
    <td style="border-bottom: solid 1px #000;" align="center">MSRP</td>
    <td style="border-bottom: solid 1px #000;" align="center">Price</td>
    <td style="border-bottom: solid 1px #000"; align="center">Price</td>
    <td style="border-bottom: solid 1px #000;" align="center">Quantity</td>
    <td style="border-bottom: solid 1px #000;" align="center">&nbsp;&nbsp;Amount</td>
  </tr>
</table>
EOD;

    $pdf->writeHTMLCell($w=0, $h=0, $x='2', $y='', $htmlselling1, $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=true);
    
    
    $sqlstmselling ="SELECT * FROM `po_dtl` WHERE `PO_No` ='".$GLOBALS['pono']."' AND `ItemLine_No` ='".$itemlineno. "'";
    $sellresultsq = mysql_query($sqlstmselling);
    
    
    $polinetotalamount =0;
    $polinetotalqty = 0;
    while($sr = mysql_fetch_array($sellresultsq)){
    
    
    $size = $sr['Size_CD'];
    $upcnean = $sr['EAN_UPC_CD'];
    $msrp = $sr['RetailPrice_1'];
    $msrpcurrency = $sr['BcurrencyCD'];
    $custsellprx = $sr['RetailPrice_2'];
    $custsellcurr = $sr['BcurrencyCD'];
    $prx = $sr['UnitPrice_3'];
    //$prxcurr = $sr[$i]['ItemLine_no'];
    $quantity = $sr['Qty'];
    $qtyuom = $sr['QtyUOM'];
    $amount = $prx * $quantity;
    $polinetotalamount += $amount;
    $polinetotalqty += $quantity;
    
    $msrp =number_format($msrp, 2, '.', ',');
    $custsellprx =number_format($custsellprx, 2, '.', ',');
    $prx =number_format($prx, 2, '.', ',');
    $amount =number_format($amount, 2, '.', ',');
    //$htmlsellingb = "htmlsellingb".$z;
$htmlsellingb = <<<EOD
<table width="750" height="138" border="0">
  <tr>
    <td align="center">$size</td>
    <td>$upcnean</td>
    <td>$msrp&nbsp;$msrpcurrency</td>
    <td>$custsellprx&nbsp;$custsellcurr </td>
    <td>$prx&nbsp;&nbsp;$resultbody[74]</td>
    <td>$quantity&nbsp;&nbsp;$qtyuom</td>
    <td align="right" width="9%">$amount</td>
  </tr>
</table>
EOD;

    $pdf->writeHTMLCell($w=0, $h=0, $x='2', $y='', $htmlsellingb, $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=true);
    
    }
    $ordertotalamount += $polinetotalamount;
    $ordertotalqty += $polinetotalqty;
    $polinetotalqty = number_format($polinetotalqty);
    $polinetotalamount = number_format($polinetotalamount, 2, '.', ',');
$htmlsellingc = <<<EOD
<table width="750" border="0" style="border-top: solid 1px #000;">
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td width="5%">&nbsp;</td>
    <td style="border-bottom: solid 1px #000;" width="18%">PO Line Total</td>
    <td style="border-bottom: solid 1px #000;" align="right" width="10%">$polinetotalqty&nbsp;&nbsp;&nbsp;</td>
    <td style="border-bottom: solid 1px #000;" align="right" width="20%">$polinetotalamount</td>
  </tr>
</table>
EOD;

    $pdf->writeHTMLCell($w=0, $h=0, $x='2', $y='', $htmlsellingc, $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=true);
    
    }
    $ordertotalqty = number_format($ordertotalqty);
    $ordertotalamount = number_format($ordertotalamount, 2, '.', ',');
$htmlsellingd = <<<EOD

<table width="752" border="0">
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td width="4%">&nbsp;</td>
    <td style="border-bottom: double 4px;" width="18%">Purchase Order Total</td>
    <td style="border-bottom: double 4px;" align="right" width="10%">$ordertotalqty&nbsp;&nbsp;&nbsp;</td>
    <td style="border-bottom: double 4px;" align="right" width="20%">$ordertotalamount</td>
  </tr>
</table>
<p>&nbsp;</p>
<u>Order Remarks / Comments</u><br />
$resultbody[16]
<br />
<br />
<p align="right">
By Purchase Agent:<u>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</u>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</p>
<p>&nbsp;</p>
<p><u>Terms And Condition:</u>
<p style="font-style: italic;font-size:8pt;">$resultbody[75]</p>
<p>***End of Purchase Order***</p>
</p>
EOD;
    $pdf->writeHTMLCell($w=0, $h=0, $x='2', $y='', $htmlsellingd, $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=true);
    
    // ---------------------------------------------------------
    $pdf1stpath = date("Ynj")."."."SAP".".".date("Hi");
    if (!is_dir("./pdfs/".$pdf1stpath)) {
        mkdir("./pdfs/".$pdf1stpath);
    }
    if (!is_dir("./pdfs/".$pdf1stpath."/".$boardNo)) {
        mkdir("./pdfs/".$pdf1stpath."/".$boardNo);
    }
    $pdfoutPutpath = "./pdfs/".$pdf1stpath."/".$boardNo."/";
    $pdfoputFileName = $boardNo."_".$resultbody[17]."_".$GLOBALS['pono'];
    $pdf->Output($pdfoutPutpath.$pdfoputFileName.".pdf", "F");
    return "Already gen # ".$pdfoputFileName.".pdf";
    //============================================================+
    // END OF FILE                                                
    //============================================================+   

    }
    else{
        
        return "not found ps number";
    }
    
    
    }
    
    
    
    




?>